package com.chanyachting.emall.serviceImpl;







import java.util.List;

import org.hibernate.HibernateException;

import com.chanyachting.emall.dao.MemberDAO;
import com.chanyachting.emall.domain.Companymember;
import com.chanyachting.emall.domain.Member;
import com.chanyachting.emall.exception.AccountExistException;
import com.chanyachting.emall.exception.AccountNotFoundException;
import com.chanyachting.emall.exception.EmailExistException;
import com.chanyachting.emall.exception.PasswordErrorException;
import com.chanyachting.emall.service.MemberService;

public class MemberServiceImpl implements MemberService {

	
	private MemberDAO mdao;
	
	
	public MemberServiceImpl(){
		
		this.mdao=new MemberDAO();
		
	}
	
	
	
	/****
	 * 会员登录
	 * @throws AccountNotFoundException 
	 * @throws PasswordErrorException 
	 * @throws HibernateException 
	 */
	public Member login(Member member) throws AccountNotFoundException, PasswordErrorException, HibernateException {
		
		
		//1.验证会员账号
		if(!this.checkAccount(member.getAccount())){
			
			throw new AccountNotFoundException();
		}
		
		//2.验证会员密码
		if(!this.checkPassword(member.getPassword(), member.getAccount())){
			
			throw new PasswordErrorException();
		}
		
		//3.登录会员
		Member newMember=mdao.getMemberByAccountAndPassword(member.getAccount(), member.getPassword());
		
		newMember.setLoginDate(member.getLoginDate());
		newMember.setLoginIP(member.getLoginIP());
		
		mdao.update(newMember);
		
		return newMember;
	}

	
	/****
	 * 会员注册
	 * @throws AccountExistException 
	 * @throws EmailExistException 
	 * @throws HibernateException 
	 */
	public boolean regist(Member member) throws AccountExistException, EmailExistException, HibernateException {
		
		
		//1.检查账号是否重复
		if(mdao.checkAccount(member.getAccount())){
			
			throw new AccountExistException();
		}
		
		
		//2.检查联系人Email是否重复
		if(mdao.checkEmail(member.getEmail())){
			
			throw new EmailExistException();
		}
		
		//3.注册会员
		mdao.save(member);
		
		return true;//member.addNewMember();
		
	}

	public Member registMember(Member member) throws AccountExistException, EmailExistException, HibernateException {
		
		
		//1.检查账号是否重复
		if(mdao.checkAccount(member.getAccount())){
			
			throw new AccountExistException();
		}
		
		
		//2.检查联系人Email是否重复
		if(mdao.checkEmail(member.getEmail())){
			
			throw new EmailExistException();
		}
		
		//3.注册会员
		mdao.save(member);
		
		return member;
		
	}
	
	/***
	 * 更新会员基本信息
	 * @throws HibernateException 
	 */
	public boolean updateMemberInfo(Member member) {
		
		
		try {
			
			mdao.update(member);
			
		} catch (Exception e) {
			
			return false;
		}
		
		return true;
	}

	
	/****
	 * 修改会员email
	 * @throws HibernateException 
	 */
	public boolean updateMemberEmail(String newEmail, Member member) throws HibernateException {
		
		return mdao.updateMemberEmail(member, newEmail);
	}

	
	/****
	 * 修改会员密码
	 * @throws HibernateException 
	 */
	public boolean updatePassword(String newPassword, Member member) throws HibernateException {
		
		return mdao.updateMemberPassword(member, newPassword);
	}

	/***
	 * 检查登录账号是否存在
	 * @throws HibernateException 
	 */
	public boolean checkAccount(String account) throws HibernateException {
		
		return mdao.checkAccount(account);
	}


	/*****
	 * 检查email 是否存在
	 * @throws HibernateException 
	 */
	public boolean checkEmail(String email) throws HibernateException {
		
		return mdao.checkEmail(email);
	}

	/****
	 * 检查密码是否与账号匹配
	 * @throws HibernateException 
	 */
	public boolean checkPassword(String password, String account) throws HibernateException {
		
		
		return mdao.checkPassword(account, password);
	}



	public Companymember getCompanyMemberByMember(Member member) {
		
		//检查会员是否是公司会员
		if(this.ifCompanyMember(member)){
			
			String args[]={String.valueOf(member.getId())};
			
			List list= this.mdao.find("from Companymember where memberId=?", args);
			
			if(list!=null && list.size()>0) return (Companymember) list.get(0);
			
		}
		
		return null;
	}



	public boolean ifCompanyMember(Member member) {
		
		if(member==null || member.getId()==null) return false;
		
		member=(Member) this.mdao.load(Member.class, member.getId());
			
		if(member.getMemberType().equals("01")) return true;
		
		return false;
	}



	public List<Member> getAllMember() {
		
		return this.mdao.find("from Member");
	}



	public Member getOneMember(Member member) {
		
		return (Member) this.mdao.load(Member.class, member.getId());
	}


}
